<!DOCTYPE html>
<html lang="zh-cn">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name="baidu-site-verification" content="code-N4HqDA2yr7" />
<meta name="msvalidate.01" content="C30AE7336C0C214E141AC1AAC39A9A05" /><meta name='description' content='LuaSnip与neovim 在neovim用lua重写之后，用lua去写代码片段，似乎成了一种天经地义的事情，于是LuaSnip诞生了，为了'><title>LuaSnip解放你的双手</title>

<link rel='canonical' href='/clean-bug/luasnip%E8%A7%A3%E6%94%BE%E4%BD%A0%E7%9A%84%E5%8F%8C%E6%89%8B/'>

<link rel="stylesheet" href="/scss/style.min.732a6bd9762caf6dce004132442f09bcd64101bf70fd8ab25f9c10d85d58f6b7.css"><meta property='og:title' content='LuaSnip解放你的双手'>
<meta property='og:description' content='LuaSnip与neovim 在neovim用lua重写之后，用lua去写代码片段，似乎成了一种天经地义的事情，于是LuaSnip诞生了，为了'>
<meta property='og:url' content='/clean-bug/luasnip%E8%A7%A3%E6%94%BE%E4%BD%A0%E7%9A%84%E5%8F%8C%E6%89%8B/'>
<meta property='og:site_name' content='倔强的贝吉塔'>
<meta property='og:type' content='article'><meta property='article:section' content='Clean-Bug' /><meta property='article:tag' content='日常记录' /><meta property='article:tag' content='neovim' /><meta property='article:tag' content='LuaSnip' /><meta property='article:published_time' content='2022-12-16T23:01:34&#43;08:00'/><meta property='article:modified_time' content='2022-12-16T23:01:34&#43;08:00'/><meta property='og:image' content='/categories/clean-bug/bug.png' />
<meta name="twitter:title" content="LuaSnip解放你的双手">
<meta name="twitter:description" content="LuaSnip与neovim 在neovim用lua重写之后，用lua去写代码片段，似乎成了一种天经地义的事情，于是LuaSnip诞生了，为了"><meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image" content='/categories/clean-bug/bug.png' />
    <link rel="shortcut icon" href="/img/sorcerer.png" />

<script async src="https://www.googletagmanager.com/gtag/js?id=G-V4H6ZTRWB4"></script>
<script>
var doNotTrack = false;
if (!doNotTrack) {
	window.dataLayer = window.dataLayer || [];
	function gtag(){dataLayer.push(arguments);}
	gtag('js', new Date());
	gtag('config', 'G-V4H6ZTRWB4', { 'anonymize_ip': false });
}
</script>

            
<script async src="https://www.googletagmanager.com/gtag/js?id=G-V4H6ZTRWB4"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-V4H6ZTRWB4');
</script>

    </head>
    <body class="
    article-page has-toc
">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "dark");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex 
    
        extended
    
">
    
        <div id="article-toolbar">
            <a href="/" class="back-home">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="15 6 9 12 15 18" />
</svg>



                <span>返回</span>
            </a>
        </div>
    
<main class="main full-width">
    <article class="has-image main-article">
    <header class="article-header">
        <div class="article-image">
            <a href="/clean-bug/luasnip%E8%A7%A3%E6%94%BE%E4%BD%A0%E7%9A%84%E5%8F%8C%E6%89%8B/">
                
                    <img src="/categories/clean-bug/bug.png" loading="lazy" alt="Featured image of post LuaSnip解放你的双手" />
                
            </a>
        </div>
    

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="/categories/clean-bug/" >
                clean-bug
            </a>
        
    </header>
    

    <h2 class="article-title">
        <a href="/clean-bug/luasnip%E8%A7%A3%E6%94%BE%E4%BD%A0%E7%9A%84%E5%8F%8C%E6%89%8B/">LuaSnip解放你的双手</a>
    </h2>

    

    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">2022-12-16</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    阅读时长: 5 分钟
                </time>
            </div>
        
    </footer>
    
</div>
</header>

    <section class="article-content">
    
    
    <h2 id="luasnip与neovim">LuaSnip与neovim</h2>
<p>在neovim用lua重写之后，用lua去写代码片段，似乎成了一种天经地义的事情，于是<a class="link" href="https://github.com/L3MON4D3/LuaSnip"  target="_blank" rel="noopener"
    >LuaSnip</a>诞生了，为了兼容之前各种snippet插件的格式，LuaSnip允许你使用vscode以及SnipMate（UltiSnip）的格式去写代码片段，但对这些格式做了一定的扩展和修改（兼容了，但没完全兼容），这里以vscode代码片段为例，大致展示一些常用的写法：</p>
<h3 id="动态文本输入dynamic-node">动态文本输入（Dynamic Node）</h3>
<p>比如在C++中，经常在头文件开始处写下这样的宏定义，来防止头文件被重复引用</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="cl"><span class="cp">#ifndef _XXX_H
</span></span></span><span class="line"><span class="cl"><span class="cp">#define _XXX_H
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="c1">// write your code
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="cp">#endif </span><span class="c1">//_XXX_H
</span></span></span></code></pre></div><p>此类代码片段中包含重复的输入（<code>_XXX_H</code>），我们并不想重复输入这类文本，因此我们可以定义如下代码片段：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl">  <span class="s2">&#34;Header&#34;</span><span class="err">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;prefix&#34;</span><span class="p">:</span> <span class="s2">&#34;ifn&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;body&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;#ifndef ${1:_HEADER_H}&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;#define $1&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;$0&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;#endif //$1&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;description&#34;</span><span class="p">:</span> <span class="s2">&#34;add _HEADER_H&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span></code></pre></div><p><figure 
	>
	<a href="/img/luasnip-img/DynamicNode.gif" >
		<img src="/img/luasnip-img/DynamicNode.gif"
			
			
			
			loading="lazy"
			alt="DynamicNode">
	</a>
	
	<figcaption>DynamicNode</figcaption>
	
</figure></p>
<h4 id="参数解释">参数解释</h4>
<ul>
<li>
<p>&ldquo;Header&rdquo;: 该代码片段的名字</p>
</li>
<li>
<p>&ldquo;prefix&rdquo;: 触发单词</p>
</li>
<li>
<p>&ldquo;body&rdquo;: 代码片段展开主体，多行语句写在中括号里，用逗号分隔每一行语句，<code>$1</code>是光标第一次出现的位置，<code>$0</code>是光标最后一次出现的位置，<code>${1:_HEADER_H}</code>中的<code>_HEADER_H</code>为占位符，默认无输入时填充<code>_HEADER_H</code></p>
</li>
<li>
<p>&ldquo;description&rdquo;: 代码片段的解释文档</p>
</li>
</ul>
<h3 id="选项之间跳转choicenode">选项之间跳转（ChoiceNode）</h3>
<p>有时候，我们的变量只会取到某几个固定值（比如bool类型：true和false两个取值），我们希望可以直接改变变量的值，让其在可能的取值之间变化，此时我们可以定义如下代码片段：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl">  <span class="s2">&#34;true&#34;</span><span class="err">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;prefix&#34;</span><span class="p">:</span> <span class="s2">&#34;true&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;body&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;${1|true,false|}&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;description&#34;</span><span class="p">:</span> <span class="s2">&#34;true -&gt; false&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span><span class="err">,</span>
</span></span><span class="line"><span class="cl">  <span class="s2">&#34;false&#34;</span><span class="err">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;prefix&#34;</span><span class="p">:</span> <span class="s2">&#34;false&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;body&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;${1|false,true|}&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;description&#34;</span><span class="p">:</span> <span class="s2">&#34;false -&gt; true&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span></code></pre></div><p><figure 
	>
	<a href="/img/luasnip-img/ChoiceNode.gif" >
		<img src="/img/luasnip-img/ChoiceNode.gif"
			
			
			
			loading="lazy"
			alt="ChoiceNode">
	</a>
	
	<figcaption>ChoiceNode</figcaption>
	
</figure></p>
<h3 id="复用选择文本">复用选择文本</h3>
<p>当我们复制了一个图片的链接，并把它粘贴到markdown中，突然发现自己忘记写<code>![](link_img)</code>，我们希望可以直接选中链接，然后将其补全成正确的格式，此时我们可以定义如下代码片段：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl">  <span class="s2">&#34;image&#34;</span><span class="err">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;prefix&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;img&#34;</span><span class="p">,</span> <span class="s2">&#34;!&#34;</span><span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;body&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">      <span class="s2">&#34;![${1:img_name}]($TM_SELECTED_TEXT)&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;description&#34;</span><span class="p">:</span> <span class="s2">&#34; -&gt; image&#34;</span>
</span></span></code></pre></div><p><figure 
	>
	<a href="/img/luasnip-img/visual.gif" >
		<img src="/img/luasnip-img/visual.gif"
			
			
			
			loading="lazy"
			alt="selected text">
	</a>
	
	<figcaption>selected text</figcaption>
	
</figure></p>
<p>这里我们可以使用img或者!来触发补全</p>
<p>以上这些代码片段基本可以满足我们平时写代码的大部分需求，vscode snippet的语法也比较简单易懂，但是还有一些特殊情况，我们直接使用LuaSnip自己的语法去写，可能会更方便，比如通过自动触发补全来达到文本替换的效果。</p>
<p>vscode去写类似片段的画风是这样的：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="s2">&#34;Namespace Src&#34;</span><span class="err">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;prefix&#34;</span><span class="p">:</span> <span class="s2">&#34;ns_src&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;body&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">        <span class="s2">&#34;namespace ${RELATIVE_FILEPATH/^(?:.*[\\\\\\/])?(src)(?=[\\\\\\/])|[\\\\\\/][^\\\\\\/]*$|([\\\\\\/])/${1:+Src}${2:+\\\\}/g};&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">     <span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;description&#34;</span><span class="p">:</span> <span class="s2">&#34;Namespace for file routes inside the src folder&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span><span class="err">,</span>
</span></span></code></pre></div><p><a class="link" href="https://stackoverflow.com/questions/70869062/how-can-i-create-a-vscode-snippet-to-automatically-insert-namespace-of-files-ins"  target="_blank" rel="noopener"
    >How can I create a VSCode snippet to automatically insert namespace of files inside my src folder?</a></p>
<p>这里展示一个LuaSnip编写的文本替换的例子，不按shift键输入数字键上方对应的符号：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">shift_switch</span> <span class="o">=</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span><span class="s1">&#39;4&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kr">function</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">return</span> <span class="s1">&#39;$&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span><span class="s1">&#39;5&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kr">function</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">return</span> <span class="s1">&#39;%&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span><span class="s1">&#39;6&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kr">function</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">return</span> <span class="s1">&#39;^&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span><span class="s1">&#39;7&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kr">function</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">return</span> <span class="s1">&#39;&amp;&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span><span class="s1">&#39;8&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kr">function</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">return</span> <span class="s1">&#39;*&#39;</span>
</span></span><span class="line"><span class="cl">  <span class="kr">end</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">ls.add_snippets</span><span class="p">(</span><span class="s2">&#34;all&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="n">s</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="n">trig</span> <span class="o">=</span> <span class="s2">&#34;([4-8]);;&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="n">regTrig</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl">    <span class="n">f</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">snip</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">      <span class="n">print</span><span class="p">(</span><span class="n">snip.captures</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">      <span class="kr">return</span> <span class="n">shift_switch</span><span class="p">[</span><span class="n">snip.captures</span><span class="p">[</span><span class="mi">1</span><span class="p">]]()</span>
</span></span><span class="line"><span class="cl">    <span class="kr">end</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl">  <span class="p">)},</span>
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">type</span> <span class="o">=</span> <span class="s2">&#34;autosnippets&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="n">key</span> <span class="o">=</span> <span class="s2">&#34;shift&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span></code></pre></div><p><figure 
	>
	<a href="/img/luasnip-img/autotrig.gif" >
		<img src="/img/luasnip-img/autotrig.gif"
			
			
			
			loading="lazy"
			alt="autotrig">
	</a>
	
	<figcaption>autotrig</figcaption>
	
</figure></p>
<h2 id="luasnip代码片段的基本形式">LuaSnip代码片段的基本形式</h2>
<blockquote>
<p>更多教程查阅LuaSnip官方文档：https://zjp-cn.github.io/neovim0.6-blogs/nvim/luasnip/doc1.html</p>
</blockquote>
<p>在 LuaSnip 中，代码片段由节点 (nodes) 组成。节点包括：</p>
<ul>
<li>
<p>textNode：静态文本</p>
</li>
<li>
<p>insertNode：可编辑的文本</p>
</li>
<li>
<p>functionNode：函数节点，可从其他节点的内容生成的文本</p>
</li>
<li>
<p>dynamicNode：动态节点，基于输入生成的节点</p>
</li>
<li>
<p>其他节点</p>
<ul>
<li>choiceNode：在两个节点（或更多节点）之间进行选择</li>
<li>restoreNode：存储和恢复到节点的输入</li>
</ul>
</li>
</ul>
<h2 id="luasnip代码片段编写">LuaSnip代码片段编写</h2>
<p>一些变量的定义：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">ls</span> <span class="o">=</span> <span class="n">require</span> <span class="s2">&#34;luasnip&#34;</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">s</span> <span class="o">=</span> <span class="n">ls.snippet</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">sn</span> <span class="o">=</span> <span class="n">ls.snippet_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">isn</span> <span class="o">=</span> <span class="n">ls.indent_snippet_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">t</span> <span class="o">=</span> <span class="n">ls.text_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">i</span> <span class="o">=</span> <span class="n">ls.insert_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">f</span> <span class="o">=</span> <span class="n">ls.function_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">c</span> <span class="o">=</span> <span class="n">ls.choice_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">d</span> <span class="o">=</span> <span class="n">ls.dynamic_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">r</span> <span class="o">=</span> <span class="n">ls.restore_node</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">events</span> <span class="o">=</span> <span class="n">require</span> <span class="s2">&#34;luasnip.util.events&#34;</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">ai</span> <span class="o">=</span> <span class="n">require</span> <span class="s2">&#34;luasnip.nodes.absolute_indexer&#34;</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">extras</span> <span class="o">=</span> <span class="n">require</span> <span class="s2">&#34;luasnip.extras&#34;</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">fmt</span> <span class="o">=</span> <span class="n">extras.fmt</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">m</span> <span class="o">=</span> <span class="n">extras.m</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">l</span> <span class="o">=</span> <span class="n">extras.l</span>
</span></span><span class="line"><span class="cl"><span class="kd">local</span> <span class="n">postfix</span> <span class="o">=</span> <span class="n">require</span> <span class="s2">&#34;luasnip.extras.postfix&#34;</span><span class="p">.</span><span class="n">postfix</span>
</span></span></code></pre></div><ul>
<li>
<p>t:  静态文本</p>
</li>
<li>
<p>i: 可编辑的文本</p>
</li>
<li>
<p>f: 函数节点，可从其他节点的内容生成的文本</p>
</li>
<li>
<p>d: 动态节点，基于输入生成的节点</p>
</li>
<li>
<p>c: 在两个节点（或更多节点）之间进行选择</p>
</li>
<li>
<p>r: 存储和恢复到节点的输入</p>
</li>
</ul>
<h3 id="textnode">textNode</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">s</span><span class="p">(</span><span class="s2">&#34;trigger&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">t</span><span class="p">({</span><span class="s2">&#34;first line&#34;</span><span class="p">,</span> <span class="s2">&#34;second line&#34;</span><span class="p">})</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span></code></pre></div><p>多行字符串可以通过 <strong>花括号包裹多个字符串</strong>（table） 来定义，因此换行可以用<code>t({&quot;&quot;, &quot;&quot;})</code>来表示。</p>
<h3 id="insertnode">insertNode</h3>
<p>这种节点包含可编辑的文本，并且可以跳进和跳出（像 vscode snippets 中的 $1）。</p>
<h3 id="functionnode">functionNode</h3>
<p>FunctionNode （函数节点） 根据其他节点的内容，使用自定义的函数来插入文本，格式如下：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">ls.add_snippets</span><span class="p">(</span><span class="s2">&#34;all&#34;</span><span class="p">,</span> 
</span></span><span class="line"><span class="cl">  <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">s</span><span class="p">(</span><span class="s2">&#34;trig4&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="n">i</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s2">&#34;text_of_first &#34;</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">      <span class="n">i</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">{</span> <span class="s2">&#34;first_line_of_second&#34;</span><span class="p">,</span> <span class="s2">&#34;second_line_of_second&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span> <span class="p">}),</span>
</span></span><span class="line"><span class="cl">      <span class="n">f</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="kr">function</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">snip</span><span class="p">,</span> <span class="n">user_args1</span><span class="p">,</span> <span class="n">user_args2</span><span class="p">)</span> <span class="kr">return</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">..</span> <span class="s2">&#34; &#34;</span> <span class="o">..</span> <span class="n">args</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">..</span> <span class="n">user_args1</span> <span class="o">..</span> <span class="n">user_args2</span> <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span> <span class="p">},</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span><span class="n">user_args</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">&#34; this is user_args&#34;</span><span class="p">,</span> <span class="s2">&#34; end&#34;</span><span class="p">}</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">})</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span></code></pre></div><p>functionNode有三个参数:</p>
<ul>
<li>
<p>函数</p>
<ul>
<li>args: 获取指定节点索引处的文本, 例如我们传递1号索引，2号索引（注意，此处有先后顺序的区别，{1，2}是先传1后传2，{2，1}则相反），args获取的参数相当于一个表格，行索引对应args的第一维，列索引对应args的第二维，因此<code>args[2][1] = first_line_of_second</code></li>
</ul>
<pre tabindex="0"><code>| text_of_first         | first_line_of_second |
| second_line_of_second |                      |
</code></pre><p>如果我们传递的节点索引为{2, 1}，则args获取的参数为</p>
<pre tabindex="0"><code>| first_line_of_second  | text_of_first        |
| second_line_of_second |                      |
</code></pre><ul>
<li>
<p>snip: snip 为函数节点的直系父节点。它能轻松访问函数节点中可能有用的任何内容，比如触发条件中正则表达式匹配的内容：<code>trig = &quot;([4-8]);;&quot;</code>，<code>snip.captures</code>可以获取当前匹配的数字具体是几。</p>
</li>
<li>
<p>user_arg1[user_args2, &hellip;, user_argsn]: 用户参数，可通过user_args传递</p>
</li>
</ul>
</li>
<li>
<p>节点索引：args中提到的节点索引，示例中的{1, 2}</p>
</li>
<li>
<p>用户参数【可选】：传递user_args1，user_args2用</p>
</li>
</ul>
<p>代码片段展开结果：</p>
<pre tabindex="0"><code>text_of_first first_line_of_second
second_line_of_second
text_of_first first_line_of_second this is user_args end
</code></pre><h3 id="choicenode">ChoiceNode</h3>
<p>ChoiceNode 允许在多个节点之间进行选择。</p>
<p>通过调用 ls.change_choice(1) (向前) 或 ls.change_choice(-1) (向后) 来更改 ChoiceNode 的当前选择，这里我的绑定按键是<code>ctrl+j</code>与<code>ctrl+k</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">vim.api</span><span class="p">.</span><span class="n">nvim_set_keymap</span><span class="p">(</span><span class="s2">&#34;i&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;C-j&gt;&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;Plug&gt;luasnip-next-choice&#34;</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl"><span class="n">vim.api</span><span class="p">.</span><span class="n">nvim_set_keymap</span><span class="p">(</span><span class="s2">&#34;s&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;C-j&gt;&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;Plug&gt;luasnip-next-choice&#34;</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl"><span class="n">vim.api</span><span class="p">.</span><span class="n">nvim_set_keymap</span><span class="p">(</span><span class="s2">&#34;i&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;C-k&gt;&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;Plug&gt;luasnip-prev-choice&#34;</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl"><span class="n">vim.api</span><span class="p">.</span><span class="n">nvim_set_keymap</span><span class="p">(</span><span class="s2">&#34;s&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;C-k&gt;&#34;</span><span class="p">,</span> <span class="s2">&#34;&lt;Plug&gt;luasnip-prev-choice&#34;</span><span class="p">,</span> <span class="p">{})</span>
</span></span></code></pre></div><p>个人认为vscode snippet的choiceNode更容易编写，我可能会更偏向于通过vscode snippet的方式去实现ChoiceNode，因此这里就不解释ChoiceNode的相关函数了，具体解释请移步 <a class="link" href="https://zjp-cn.github.io/neovim0.6-blogs/nvim/luasnip/doc1.html#choicenode"  target="_blank" rel="noopener"
    >ChoiceNode</a></p>
<p>不过我还是写了<a class="link" href="#luasnip-choicenode" >LuaSnip版ChoiceNode示例</a></p>
<p>更多配置参见 <a class="link" href="https://github.com/StubbornVegeta/nvim"  target="_blank" rel="noopener"
    >StubbornVegeta&rsquo;s neovim config</a></p>
<h2 id="附录">附录</h2>
<h3 id="luasnip-dynamicnode">[LuaSnip] DynamicNode</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">ls.add_snippets</span><span class="p">(</span><span class="s2">&#34;all&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">s</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">      <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">trig</span> <span class="o">=</span> <span class="s2">&#34;ifndef&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="n">dscr</span> <span class="o">=</span> <span class="s2">&#34;ifndef .. define .. endif&#34;</span>
</span></span><span class="line"><span class="cl">      <span class="p">},</span>
</span></span><span class="line"><span class="cl">      <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">t</span><span class="p">({</span><span class="s2">&#34;#ifndef &#34;</span><span class="p">}),</span> <span class="n">i</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span><span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">f</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">          <span class="kr">function</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">snip</span><span class="p">,</span> <span class="n">user_args_1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">            <span class="kr">return</span>  <span class="n">user_args_1</span> <span class="o">..</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">          <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">          <span class="p">{</span> <span class="mi">1</span> <span class="p">},</span>
</span></span><span class="line"><span class="cl">          <span class="p">{</span> <span class="n">user_args</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&#34;#define &#34;</span> <span class="p">}</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">),</span> <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span><span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">i</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;#endif&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span> <span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">i</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">      <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span></code></pre></div><h3 id="luasnip-choicenode">[LuaSnip] ChoiceNode</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">ls.add_snippets</span><span class="p">(</span><span class="s2">&#34;c&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="n">s</span><span class="p">(</span><span class="s2">&#34;true&#34;</span><span class="p">,</span> <span class="n">c</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">i</span><span class="p">(</span><span class="kc">nil</span><span class="p">,</span> <span class="s2">&#34;true&#34;</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">    <span class="n">f</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="kr">return</span> <span class="s2">&#34;false&#34;</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl">  <span class="p">})),</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="n">s</span><span class="p">(</span><span class="s2">&#34;false&#34;</span><span class="p">,</span> <span class="n">c</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">i</span><span class="p">(</span><span class="kc">nil</span><span class="p">,</span> <span class="s2">&#34;false&#34;</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">    <span class="n">f</span><span class="p">(</span><span class="kr">function</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="kr">return</span> <span class="s2">&#34;true&#34;</span> <span class="kr">end</span><span class="p">,</span> <span class="p">{})</span>
</span></span><span class="line"><span class="cl">  <span class="p">}))</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span></code></pre></div><h3 id="luasnip-复用选择文本">[LuaSnip] 复用选择文本</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-lua" data-lang="lua"><span class="line"><span class="cl"><span class="n">ls.add_snippets</span><span class="p">(</span><span class="s2">&#34;all&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">s</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">      <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">trig</span> <span class="o">=</span> <span class="s2">&#34;ifndef&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="n">dscr</span> <span class="o">=</span> <span class="s2">&#34;ifndef .. define .. endif&#34;</span>
</span></span><span class="line"><span class="cl">      <span class="p">},</span>
</span></span><span class="line"><span class="cl">      <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">t</span><span class="p">({</span><span class="s2">&#34;#ifndef &#34;</span><span class="p">}),</span> <span class="n">i</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span><span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">f</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">          <span class="kr">function</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">snip</span><span class="p">,</span> <span class="n">user_args_1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">            <span class="kr">return</span>  <span class="n">user_args_1</span> <span class="o">..</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">          <span class="kr">end</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">          <span class="p">{</span> <span class="mi">1</span> <span class="p">},</span>
</span></span><span class="line"><span class="cl">          <span class="p">{</span> <span class="n">user_args</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&#34;#define &#34;</span> <span class="p">}</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">),</span> <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span><span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">i</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">t</span><span class="p">({</span><span class="s2">&#34;&#34;</span><span class="p">,</span> <span class="s2">&#34;#endif&#34;</span><span class="p">,</span> <span class="s2">&#34;&#34;</span> <span class="p">}),</span>
</span></span><span class="line"><span class="cl">        <span class="n">i</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">      <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">)</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span></code></pre></div>
</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/%E6%97%A5%E5%B8%B8%E8%AE%B0%E5%BD%95/">日常记录</a>
        
            <a href="/tags/neovim/">neovim</a>
        
            <a href="/tags/luasnip/">LuaSnip</a>
        
    </section>


    
    <section class="article-copyright">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copyright" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <path d="M14.5 9a3.5 4 0 1 0 0 6" />
</svg>



        <span>Licensed under CC BY-NC-SA 4.0</span>
    </section>
    

</footer>


    
        <link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/katex.min.css"integrity="sha256-J&#43;iAE0sgH8QSz9hpcDxXIftnj65JEZgNhGcgReTTK9s="crossorigin="anonymous"
            ><script 
                src="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/katex.min.js"integrity="sha256-InsNdER1b2xUewP&#43;pKCUJpkhiqwHgqiPXDlIk7GzBu4="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/contrib/auto-render.min.js"integrity="sha256-y39Mpg7V3D4lhBX4x6O0bUqTV4pSrfgwEfGKfxkOdgI="crossorigin="anonymous"
                defer
                >
            </script><script>
    window.addEventListener("DOMContentLoaded", () => {
        renderMathInElement(document.querySelector(`.article-content`), {
            delimiters: [
                { left: "$$", right: "$$", display: true },
                { left: "$", right: "$", display: false },
                { left: "\\(", right: "\\)", display: false },
                { left: "\\[", right: "\\]", display: true }
            ]
        });})
</script>
    
</article>

    

    <aside class="related-contents--wrapper">
    
    
        <h2 class="section-title"></h2>
        <div class="related-contents">
            <div class="flex article-list--tile">
                
                    
<article class="has-image">
    <a href="/clean-bug/ssh%E7%99%BB%E5%BD%95%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E4%B8%8D%E7%94%9F%E6%95%88/">
        
        
            <div class="article-image">
                
                    <img src="/categories/clean-bug/bug.png" loading="lazy" data-key="" data-hash="/categories/clean-bug/bug.png"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">ssh登录环境变量不生效</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="/clean-bug/%E4%BB%A5%E9%9D%9Eroot%E8%BA%AB%E4%BB%BD%E6%89%A7%E8%A1%8Cdocker/">
        
        
            <div class="article-image">
                
                    <img src="/categories/clean-bug/bug.png" loading="lazy" data-key="" data-hash="/categories/clean-bug/bug.png"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">以非root身份执行docker</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="/clean-bug/wsl2%E4%B8%8Bdocker%E5%90%AF%E5%8A%A8%E9%97%AE%E9%A2%98/">
        
        
            <div class="article-image">
                
                    <img src="/categories/clean-bug/bug.png" loading="lazy" data-key="" data-hash="/categories/clean-bug/bug.png"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">wsl2下docker启动问题</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="/p/%E6%97%A5%E5%B8%B8%E8%AE%B0%E5%BD%95%E4%B8%8Ebug%E5%A4%84%E7%90%86/">
        
        
            <div class="article-image">
                <img src="/p/%E6%97%A5%E5%B8%B8%E8%AE%B0%E5%BD%95%E4%B8%8Ebug%E5%A4%84%E7%90%86/bug.0eb81f4fc2c6c306c0254759192da7f4_hu081a353cc6bb62ce4c97494e3c8ce280_27096_250x150_fill_box_smart1_3.png" 
                        width="250" 
                        height="150" 
                        loading="lazy"
                        alt="Featured image of post 日常记录与bug处理"
                        
                        data-hash="md5-DrgfT8LGwwbAJUdZGS2n9A==">
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">日常记录与bug处理</h2>
        </div>
    </a>
</article>
                
                    
<article class="has-image">
    <a href="/clean-bug/macos%E4%B8%8Bzsh%E7%BB%88%E7%AB%AFbrew%E6%97%A0%E6%B3%95%E8%A1%A5%E5%85%A8/">
        
        
            <div class="article-image">
                
                    <img src="/categories/clean-bug/bug.png" loading="lazy" data-key="" data-hash="/categories/clean-bug/bug.png"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">MacOS下zsh终端brew无法补全</h2>
        </div>
    </a>
</article>
                
            </div>
        </div>
    
</aside>

     
    
        
    <script src='//unpkg.com/valine@latest/dist/Valine.min.js'></script>

<div id="vcomments"></div>
<style>
    #vcomments .vcards .vcard {
        background-color: var(--card-background);
	    padding: 25px 25px 5px 25px;
	    border-radius: 3px;
	    margin-bottom: 15px;
	    transition: all .3s
    }

    #vcomments .vcards .vh .vquote .vcard {
	    padding: 10px 25px 0px 0px;
    }

    #vcomments .vcards .vcard .vh .vcard {
        border: none;
        box-shadow: none;
    }

    #vcomments .vcards .vcard .vquote .vimg {
        width: 50px;
        height: 50px;
    }

    #vcomments .vcards .vcard .vimg {
        width: 60px;
        height: 60px;
        float: left;
        border-radius: 5px;
        margin-right: 18px;
        border: 0px;
        padding: 0px;
        box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.04), 0px 2px 6px rgba(0, 0, 0, 0.04), 0px 0px 1px rgba(0, 0, 0, 0.04);
    }

    #vcomments .vcards .vcard .vcontent p{
        font-size: 1.6rem;
        color: var(--card-text-color-main);
    }

    #vcomments .vcards .vcard .vcontent{
        padding-top: 0px;
    }
    
    #vcomments .vcount {
        padding: 0px;
        margin-bottom: 5px;
        margin-top: 20px;
        font-weight: 700;
        font-size: 1.4rem;
        color: var(--body-text-color);
        text-transform: uppercase;
    }

    #vcomments .vwrap .cancel-reply-btn {
        position: relative;
        
    }

    #vcomments .vreply-wrapper .vwrap {
        box-shadow: var(--shadow-l2);
        border: 1px solid rgba(143, 143, 143, 0.3);
        width: 95%;
    }

    #vcomments .vquote .vreply-wrapper .vwrap {
        width: 93%;
    }

    #vcomments .vwrap {
        background-color: var(--card-background);
        border: 0px solid #f0f0f0;
        border-radius: 3px;
        margin-bottom: 10px;
        overflow: hidden;
        position: relative;
        padding: 0px 0px 0px 0px;
        font-family: inherit;
        display: flex;
        flex-direction: column;
    }

    #vcomments .vwrap .cancel-reply {
        position: absolute;
        z-index:999;
        float: right;
        top: -20px;
        right: 0px;
    }

    #vcomments .vwrap .vheader.item3{
        order: 0;
    }

    #vcomments .vwrap .edit{
        order: 1;
    }

    #vcomments .vwrap .vinput.vpreview{
        order: 2;
    }

    #vcomments .vwrap .vemojis{
        order: 3;
    }

    #vcomments .vwrap .vrow{
        order: 4;
    }

    #vcomments .vwrap .vmark{
        order: 5;
    }

    #vcomments .vpanel {
        order: 0;
    }

    #vcomments .vload-top.text-center {
        padding-top: 20px;
        order: 1;
    }

    #vcomments .vcount {
        order: 2;
    }

    #vcomments .vcards {
        order: 3;
    }

    #vcomments .vload-bottom.text-center {
        order: 4;
    }

    #vcomments .vempty {
        order: 5;
    }

    #vcomments .vpage.txt-center {
        order: 6;
    }

    #vcomments .vpower.txt-right {
        order: 7;
    }
    
    #vcomments {
        display: flex;
        flex-direction: column;
    }

    #vcomments .vcards .vcard .vhead .vnick {
        color: var(--accent-color);
        position: relative;
        font-size: 1.6rem;
        font-weight: 700;
        margin-right: .875em;
        cursor: pointer;
        text-decoration: none;
        display: inline-block;
    }
    #vcomments .vcards .vcard .vh .vtime {
        color: var(--card-text-color-secondary);
        font-weight: lighter;
        font-size: 1.3rem;
        margin-right: .875em;
    }

    #vcomments .veditor {
        font-size: 1.6rem;
        color: var(--card-text-color-main);
        padding: 20px;
        max-height: 350px;
        resize: none;
    }
    
    #vcomments .vedit {
        border-bottom: 1px solid rgba(143, 143, 143, 0.3);
        background-color: rgba(255, 255, 255, 0.08);
        padding-top: 0px;
    }    

    #vcomments .vrow {
        padding: 20px;
    }

    #vcomments .vedit .vrow {
        padding: 0px 20px 5px 20px;
    }

    #vcomments .vicon.vemoji-btn {
        fill: var(--card-text-color-tertiary);
    }

    #vcomments .vicon.vpreview-btn {
        display:none !important;
    }

    #vcomments .vwrap .vemojis {
        padding: 20px;
    }

    #vcomments .vquote .vmeta .vat {
        right: 30px;
    }

    #vcomments .vmeta .vat {
        background: 0 0;
        cursor: pointer;
        border: 0;
        border-radius: 5px;
        text-transform: uppercase;
        color: rgba(255, 255, 255, 0.5);
        transition: box-shadow .3s ease;
        height: 60px;
        width: 60px;
        display: inline-flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        top: -20px;
        right: 10px;
    }

    #vcomments .vmeta .vat svg {
        stroke-width: 1.33;
    }
    
    #vcomments .vmeta .vat:hover {
        outline: 0;
        box-shadow: var(--shadow-l2);
    }

    #vcomments .vheader {
        display: flex;
    }

    #vcomments .vheader p {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        flex: 1;
        position: relative;
        margin: 0;
    }

    #vcomments .vheader label {
        position: absolute;
        top: 10px;
        left: 20px;
        color: var(--card-text-color-tertiary);
        margin-top: -4px;
        font-size: 14px;
    }

    #vcomments .vheader input {
         
        color: var(--card-text-color-main);
        border: 0;
        background: 0 0;
        padding: 35px 20px 15px;
        width: 100%;
        line-height: 1;
        transition: border .3s ease;
        font-size: 100%;
    }

    #vcomments .vheader p:not(:last-of-type) {
        border-right: 1px solid rgba(143, 143, 143, 0.3);
    }
    

    #vcomments .vh .vhead {
        padding-top: 5px;
    }

    #vcomments .vh .vquote .vhead {
        padding-top: 0px;
    }

    #vcomments .vbtn {
        color: var(--card-text-color-main);
        font-weight: 700;
        font-size: 1.0em;
        border-radius: 3px;
        border-color: 1px solid #666666;
    }

    #vcomments .vmore.vbtn {
        font-size: 13px;
    }

    #vcomments .vbtn:hover {
        color: #bebebe;
        border-color: #707070;
    }

    #vcomments .vbtn:active {
        color: #bebebe;
        border-color: #707070;
    }

    #vcomments .vcards .vcard .vcontent.expand {
        cursor: auto;
        max-height: none;
        overflow: visible;
    }

    #vcomments .vcards .vcard .vcontent.expand::after {
        display:none !important;
    }

    #vcomments .vcards .vcard .vcontent.expand::before {
        
        display:none !important;
    }

    #vcomments .vsys {
        display:none !important;
    }

    #vcomments .vcards .vcard .vh {
        padding-bottom: 0px;
        border-bottom: 0px dashed #f5f5f5;
    }

    #vcomments .vcards .vcard .vquote {
        padding-top: 5px;
        padding-left: 15px;
        border-left: 0px;
        px solid rgba(187, 187, 187, 0.5);
    }

    #vcomments .vicon.actived {
        fill: var(--card-text-color-main);
    }


</style>

<script>
    
    let placeholder = `发表你的评论\n`

    var valine = new Valine();

    valine.init({
        el: '#vcomments',
        appId: 'KHkYnFUrXOhcAuIDuVXPRFRy-gzGzoHsz',
        appKey: 'VR5IeFKU36DHOY3tzXQo0aBM',
        avatar: 'robohash',
        placeholder: placeholder,
        enableQQ: true,
        lang:'zh-CN',
        requiredFields: [],
        recordIP: false,
        visitor: true
    })

    html = `<p><label>昵称（必填）</label><input name='nick' placeholder='张三' class='vnick vinput' type='text'></p>
            <p><label>邮箱</label><input name='mail' placeholder='joe@example.com' class='vmail vinput' type='email'></p>
            <p><label>网址</label><input name='link' placeholder='https://' class='vlink vinput' type='text'></p>`
            
    document.querySelector(".vheader").innerHTML = html
    document.querySelector(".vpower.txt-right").innerHTML = ""

    if(window.location.hash){
        var checkExist = setInterval(function() {
           if (document.getElementById(window.location.hash.substring(1))) {
              document.getElementById(window.location.hash.substring(1)).scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"})
              clearInterval(checkExist);
           }
        }, 100);
    }

</script>


    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2022 - 
        
        2024 倔强的贝吉塔
    </section>
    
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        主题 <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.7.0">Stack</a></b> 由 <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a> 设计
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
    
        <aside class="sidebar right-sidebar sticky">
            <section class="widget archives">
                <div class="widget-icon">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



                </div>
                <h2 class="widget-title section-title">目录</h2>
                
                <div class="widget--toc">
                    <nav id="TableOfContents">
  <ol>
    <li><a href="#luasnip与neovim">LuaSnip与neovim</a>
      <ol>
        <li><a href="#动态文本输入dynamic-node">动态文本输入（Dynamic Node）</a>
          <ol>
            <li><a href="#参数解释">参数解释</a></li>
          </ol>
        </li>
        <li><a href="#选项之间跳转choicenode">选项之间跳转（ChoiceNode）</a></li>
        <li><a href="#复用选择文本">复用选择文本</a></li>
      </ol>
    </li>
    <li><a href="#luasnip代码片段的基本形式">LuaSnip代码片段的基本形式</a></li>
    <li><a href="#luasnip代码片段编写">LuaSnip代码片段编写</a>
      <ol>
        <li><a href="#textnode">textNode</a></li>
        <li><a href="#insertnode">insertNode</a></li>
        <li><a href="#functionnode">functionNode</a></li>
        <li><a href="#choicenode">ChoiceNode</a></li>
      </ol>
    </li>
    <li><a href="#附录">附录</a>
      <ol>
        <li><a href="#luasnip-dynamicnode">[LuaSnip] DynamicNode</a></li>
        <li><a href="#luasnip-choicenode">[LuaSnip] ChoiceNode</a></li>
        <li><a href="#luasnip-复用选择文本">[LuaSnip] 复用选择文本</a></li>
      </ol>
    </li>
  </ol>
</nav>
                </div>
            </section>
        </aside>
    

        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

    </body>
</html>
